// Copyright (c) 2006, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//
// Author: arvin.chen
//
//

#ifndef ALGORITHM_SORT_INSERT_H_
#define ALGORITHM_SORT_INSERT_H_

#ifndef ALGORITHM_SORT_BASE_H_
#include "algorithm/sort/base.h"
#endif

namespace algorithm {
	namespace sort {
		
		template<typename T>
		class Insert : public Base<T> {
		public:

			/**
			* Construction
			*
			* @param
			* @return
			*/
			Insert() :
				Base<T>::Base(kInsert)
			{
				
			}

			/**
			* Destruction
			*
			* @param VOID
			* @return
			*/
			~Insert()
			{

			}

			/**
			* sort
			*
			* @param T arr[]
			* @param int len
			* @return T
			*/
			virtual T* sort(T arr[], int len)
			{
				if (nullptr == arr || 1 >= len) return arr;
				for (int i = 1; i < len; i++) {
					T temp = arr[i];
					int k = i - 1;
					while(k >= 0 && arr[k] > temp) k--;
					
					//move all the position to insert k+1
					for(int j = i ; j > k + 1; j--)	arr[j] = arr[j-1];
					//insert
					arr[k+1] = temp;
				}
				return arr;
			}

		protected:
			/**
			* Assignment
			*
			* @param const Insert& cbase
			* @return Insert&
			*/
			Insert& operator=(const Insert& cbase);

			/**
			* Copy Construction
			*
			* @param const Insert& cbase
			* @return
			*/
			Insert(const Insert& cbase);

		private:

		public:
		protected:
		private:

		};
	} // namespace algorithm 
} // namespace sort


#endif  // ALGORITHM_SORT_INSERT_H_
